package org.jeecg.modules.system.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.mapper.SysTenantMapper;
import org.jeecg.modules.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

@Service("sysTenantServiceImpl")
@Slf4j
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {

    @Autowired
    ISysUserService userService;
    @Autowired
    private ISysUserDepartService userDepartService;
    @Autowired
    private ISysUserRoleService userRoleService;
    @Autowired
    private ISysDepartService departService;

    @Override
    public List<SysTenant> queryEffectiveTenant(Collection<String> idList) {
        LambdaQueryWrapper<SysTenant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysTenant::getId, idList);
        queryWrapper.eq(SysTenant::getStatus, CommonConstant.STATUS_1);
        //此处查询忽略时间条件
        return super.list(queryWrapper);
    }

    @Override
    public int countUserLinkTenant(String id) {
        LambdaQueryWrapper<SysUser> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(SysUser::getRelTenantIds, id);
        userQueryWrapper.or().like(SysUser::getRelTenantIds, "%," + id);
        userQueryWrapper.or().like(SysUser::getRelTenantIds, id + ",%");
        userQueryWrapper.or().like(SysUser::getRelTenantIds, "%," + id + ",%");
        // 查找出已被关联的用户数量
        return userService.count(userQueryWrapper);
    }

    @Override
    public boolean removeTenantById(String id) {
        // 查找出已被关联的用户数量
        int userCount = this.countUserLinkTenant(id);
        if (userCount > 0) {
            throw new JeecgBootException("该租户已被引用，无法删除！");
        }
        return super.removeById(id);
    }

    @Override
    @Transactional
    public void permissionSave(SysTenant sysTenant) {
        //当前用户身份过滤
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        sysTenant.setPinyinCode(PinyinUtil.getFirstLetter(sysTenant.getName(), ""));
        baseMapper.insert(sysTenant);

        //step.2.保存用户
        SysUser user = new SysUser();
        int tid = sysTenant.getId();
        String s = String.format("%0" + 4 + "d", tid);
        user.setUsername( "admin"+s);
        user.setRealname(sysTenant.getName()+"管理员");
        user.setPassword("12341234");
        user.setPhone(sysTenant.getPhone());

        //step.2.保存用户_微信收费
        SysUser user2 = new SysUser();
        user2.setUsername( sysTenant.getPinyinCode()+"Wxsf");
        user2.setRealname(sysTenant.getName()+"微信收费员");
        user2.setPassword("12341234");
        user2.setPhone(sysTenant.getPhone()+"0");

        SysDepart sysDepart = new SysDepart();
        sysDepart.setDepartName(sysTenant.getName());
        sysDepart.setStatus("1");
        sysDepart.setDelFlag("0");
        sysDepart.setTenantId(sysTenant.getId());
        departService.saveDepartData(sysDepart,sysUser.getUsername());

        user.setDepartIds(sysDepart.getId());
        user.setOrgCode(sysDepart.getOrgCode());
        user.setRelTenantIds(sysTenant.getId()+"");
        userService.saveUserAccout(user,"1509723259493900290",sysDepart.getId());

        user2.setDepartIds(sysDepart.getId());
        user2.setOrgCode(sysDepart.getOrgCode());
        user2.setRelTenantIds(sysTenant.getId()+"");
        userService.saveUserAccout(user2,"1509812282820390914",sysDepart.getId());

        ////step.3  保存所属部门
        //SysUserDepart userDeaprt = new SysUserDepart(user.getId(), sysDepart.getId());
        //userDepartService.save(userDeaprt);

        //step.3  保存所属部门
        //SysUserDepart userDeaprt2 = new SysUserDepart(user2.getId(), sysDepart.getId());
        //userDepartService.save(userDeaprt2);
        //
        ////step.4 保存角色-水司管理员:1509723259493900290
        //SysUserRole userRole = new SysUserRole(user.getId(), "1509723259493900290");
        //userRoleService.save(userRole);
        ////step.4 保存角色-收费员:1509812282820390914
        //SysUserRole userRole2 = new SysUserRole(user.getId(), "1509812282820390914");
        //userRoleService.save(userRole2);

    }

}
